package com._520it.crm.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com._520it.crm.domain.Account;
import com._520it.crm.service.IAccountService;
import com._520it.crm.util.JsonResult;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class AccountController {
	@Autowired
	private IAccountService service;
	@RequestMapping("/account_selectAll")
	@ResponseBody
	public List<Account> selectAll(){
		if(true){
			//throw new RuntimeException("出了问题");
		}
		return service.selectAll();
	}


	@RequestMapping("/account/list/view")
	public ModelAndView listView(
			@RequestParam(defaultValue = "1") int current,
			@RequestParam(defaultValue = "10") int size,
			String name,
			String username,
			String province,
			String city,
			String bank,
			String subBank,
			ModelAndView modelAndView){
		modelAndView.setViewName("account/list");
		List<Account> accountList = service.selectPage(current,size,name,username,province,city,bank,subBank);

		modelAndView.addObject("list",accountList);
		int count = service.selectPageCount(current,size,name,username,province,city,bank,subBank);
		int totalCount = count%size==0?count/size:count/size+1;
		modelAndView.addObject("totalCount",totalCount);
		modelAndView.addObject("current",current);
		return modelAndView;
	}
	
	@RequestMapping("/account/save")
	@ResponseBody
	public Object save(Account account){
		service.insert(account);
		return JsonResult.createSuccess();
	}
	
	@RequestMapping("/account/index")
	public String accountIndex(){
		return "account/index";
	}
	
}
